package com.example.demo.service.imp;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Employee;
import com.example.demo.mapper.EmployeeMapper;
import com.example.demo.service.EmployeeService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImp implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;


    @Override
    public Object selectByUsername(String username) {
        return employeeMapper.selectByUsername(username);
    }

    @Override
    public void insert(Employee employee) {
        employeeMapper.insert(employee);
    }

    @Override
    public void updateById(Employee employee) {
        employeeMapper.updateById(employee);
    }

    @Override
    public void deleteById(Long id) {
        employeeMapper.deleteById(id);
    }

    @Override
    public Page<Employee> selectPage(Integer pageNum, Integer pageSize, String search) {
        LambdaQueryWrapper<Employee> wrapper = Wrappers.<Employee>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.eq(Employee::getId, search).or();
            wrapper.eq(Employee::getUsername, search).or();
            wrapper.eq(Employee::getNickName, search).or();
            wrapper.eq(Employee::getAge, search).or();
            wrapper.eq(Employee::getSex, search).or();
            wrapper.eq(Employee::getTelephone, search).or();

        }
        return employeeMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public Employee loginEmployee(String username) {
        return employeeMapper.loginEmployee(username);
    }

    @Override
    public void updatePassowrd(Employee employee) {
        employeeMapper.updatePassowrd(employee);
    }

    @Override
    public int getPassowrd(int id, String password) {
        Employee employee = employeeMapper.selectById(id);
        if (employee.getPassword().equals(password)) {
            return 0;
        } else {
            return 1;
        }
    }


}
